package com.martinwj.service.impl;

import com.martinwj.dao.channel.IChannelDAO;
import com.martinwj.dao.nav.INavDAO;
import com.martinwj.entity.Channel;
import com.martinwj.entity.Nav;
import com.martinwj.service.ChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName: ChannelServiceImpl
 * @Description: TODO
 * @author: martin-wj
 * @createDate: 2020-12-18
 */
@Service
public class ChannelServiceImpl implements ChannelService {

    @Autowired
    private IChannelDAO iChannelDAO;
    @Autowired
    private INavDAO iNavDAO;

    /**
     * 查询频道列表
     */
    public List<Channel> list() {
        return iChannelDAO.list();
    }

    /**
     * 根据主键查询频道信息
     * @param id 主键
     * @return
     */
    public Channel selectById(String id) {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        return iChannelDAO.selectById(id);
    }

    /**
     * 保存频道内容
     * @param channel
     */
    public int save(Channel channel) {
        // 判断是新增还是更新
        if (StringUtils.isEmpty(channel.getId())) {
            // 新增
            iChannelDAO.insert(channel);

            // 添加进导航
            Nav nav = new Nav();
            nav.setSort("1");
            nav.setName(channel.getName());
            nav.setLink("portal/portal.action?channelId="+channel.getId());
            nav.setIsIndex("0");
            nav.setIsUse("1");
            nav.setType("system");
            nav.setChannelId(channel.getId());

            iNavDAO.insert(nav);
        } else {
            // 更新
            iChannelDAO.update(channel);

            // 更新导航
            Nav nav = new Nav();
            nav.setName(channel.getName());
            nav.setChannelId(channel.getId());

            iNavDAO.updateByChannel(nav);
        }
        return 0;
    }

    /**
     * 删除频道
     * @param id
     */
    public void delete(String id) {
        // 删除频道
        iChannelDAO.delete(id);

        // 删除导航中对应的链接
        iNavDAO.deleteByChannelId(id);
    }

    /**
     * 通过Id删除频道
     * @param id
     * @return
     */
    @Override
    public int deleteById(String id) {
        return iChannelDAO.deleteById(id);
    }

    /**
     * 添加频道信息
     * @return
     */
    @Override
    public int addList(Channel channel) {
        return iChannelDAO.addList(channel);
    }

    /**
     * 更新频道信息
     * @param channel
     * @return
     */
    @Override
    public int updateList(Channel channel) {
        return iChannelDAO.updateList(channel);
    }

}
